home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / Arch / SBOS2 / ui.c < prev   
Encoding:
C/C++ Source or Header  |  1995-02-08  |  7.1 KB  |  404 lines

  1. /* unix/ui.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* special termio discipline for sun/sgi,
  6.  * for non blocking io and such.
  7.  * These functions should not be too difficult
  8.  * to write for a PC.
  9.  */
  10. /* $Id: ui.c,v 1.1 1995/02/01 16:43:47 espie Exp $
  11.  * $Log: ui.c,v $
  12.  * Revision 1.1  1995/02/01  16:43:47  espie
  13.  * Initial revision
  14.  *
  15.  * Revision 1.1  1995/02/01  16:43:47  espie
  16.  * Initial revision
  17.  *
  18.  * Revision 4.1  1994/01/13  09:18:48  espie
  19.  * *** empty log message ***
  20.  *
  21.  * Revision 4.0  1994/01/11  18:02:31  espie
  22.  * Major change: lots of new calls.
  23.  *
  24.  * Revision 1.1  1994/01/08  04:00:36  Espie
  25.  * Initial revision
  26.  *
  27.  * Revision 1.2  1993/12/26  18:54:21  Espie
  28.  * Modified in a more consistent way.
  29.  *
  30.  * Revision 1.1  1993/12/26  00:55:53  Espie
  31.  * Initial revision
  32.  *
  33.  * Revision 3.15  1993/12/04  16:12:50  espie
  34.  * BOOL -> boolean.
  35.  *
  36.  * Revision 3.14  1993/11/17  15:31:16  espie
  37.  * *** empty log message ***
  38.  *
  39.  * Revision 3.13  1993/10/06  17:17:45  espie
  40.  * Stupid termio bug: shouldn't restore term to sanity if we don't
  41.  * know what sanity is. For instance, if we haven't modified anything.
  42.  *
  43.  * Revision 3.11  1993/07/17  12:00:30  espie
  44.  * Added other commands (numerous).
  45.  *
  46.  * Revision 3.10  1993/07/14  16:33:41  espie
  47.  * Added partial code for hpux.
  48.  *
  49.  * Revision 3.9  1993/04/28  20:14:41  espie
  50.  * My error...
  51.  *
  52.  * Revision 3.8  1993/04/25  14:50:17  espie
  53.  * cflags interpreted correctly.
  54.  *
  55.  * Revision 3.7  1993/01/16  16:23:33  espie
  56.  * Hsavolai fix.
  57.  *
  58.  * Revision 3.6  1993/01/15  14:00:28  espie
  59.  * Added bg/fg test.
  60.  *
  61.  * Revision 3.5  1993/01/06  17:58:39  espie
  62.  * Added changes for linux.
  63.  *
  64.  * Revision 3.4  1992/12/03  15:00:50  espie
  65.  * restore stty.
  66.  *
  67.  * Revision 3.3  1992/11/27  10:29:00  espie
  68.  * General cleanup
  69.  *
  70.  * Revision 3.2  1992/11/22  17:20:01  espie
  71.  * Added update_frequency call, mostly unchecked
  72.  *
  73.  * Revision 3.1  1992/11/19  20:44:47  espie
  74.  * Protracker commands.
  75.  *
  76.  */
  77.  
  78. #ifdef dec
  79. #define stub_only
  80. #endif
  81.  
  82. #if defined(linux) || defined(__386BSD__)
  83. #include <termios.h>
  84. #else
  85. #ifdef __hpux
  86. #include <sys/bsdtty.h>
  87. #endif
  88. #include <sys/termio.h>
  89. #endif
  90. #ifdef __386BSD__
  91. #include <sys/ioctl.h>
  92. #endif
  93. #ifdef __OS2__
  94. #include <os2.h>
  95. #include <sys/termio.h>
  96. #endif
  97. #include <signal.h>
  98. #include "defs.h"
  99. #include "extern.h"
  100. #include "tags.h"
  101. #include "prefs.h"
  102.  
  103.  
  104. LOCAL void nonblocking_io P((void));
  105. LOCAL void sane_tty P((void));
  106.  
  107. LOCAL void (*INIT)P((void)) = nonblocking_io;
  108.  
  109.  
  110. LOCAL int show;
  111. /* poor man's timer */
  112. LOCAL int current_pattern;
  113. LOCAL int count_pattern, count_song;
  114. #define SMALL_DELAY 25
  115.  
  116. /* do not define any stdio routines if it's known not to work */
  117.  
  118. #ifdef stub_only
  119.  
  120. int run_in_fg()
  121.    {
  122.    return TRUE;
  123.    }
  124.  
  125. LOCAL void sane_tty()
  126.    {
  127.    }
  128.  
  129. LOCAL struct tag end_marker;
  130.  
  131. struct tag *get_ui()
  132.    {
  133.    end_marker.type = TAG_END;
  134.    return &end_marker;
  135.    }
  136.  
  137. #else
  138.  
  139. LOCAL struct termio sanity;
  140. LOCAL struct termio *psanity = 0;
  141.  
  142. LOCAL int is_fg;
  143.  
  144. /* signal handler */
  145.  
  146. LOCAL void goodbye(sig)
  147. int sig;
  148.     {
  149.     static char buffer[25];
  150.  
  151.     sprintf(buffer, "Signal %d", sig);
  152.     end_all(buffer);
  153.     }
  154.  
  155. LOCAL void abort_this(sig)
  156. int sig;
  157.    {
  158.    end_all("Abort");
  159.    }
  160.  
  161. #ifdef SIGTSTP
  162. LOCAL void suspend(sig)
  163. int sig;
  164.    {
  165.    fflush(stdout);
  166.    sane_tty();
  167.    signal(SIGTSTP, SIG_DFL);
  168.    kill(0, SIGTSTP);
  169.    }
  170. #endif
  171.  
  172. int run_in_fg()
  173.    {
  174.    USHORT          Indicator;
  175.    return(VioGetAnsi(&Indicator,0)==0);
  176.    }
  177.  
  178. LOCAL void switch_mode()
  179.    {
  180.    struct termio zap;
  181.  
  182. #ifdef SIGTSTP
  183.    signal(SIGTSTP, suspend);
  184. #endif
  185.    signal(SIGINT, goodbye);
  186.    signal(SIGQUIT, goodbye);
  187.    signal(SIGUSR1, abort_this);
  188.  
  189.    if (run_in_fg())
  190.       {
  191.       ioctl(fileno(stdin), TCGETA, &zap);
  192.       zap.c_cc[VEOL] = 0;
  193.       zap.c_cc[VEOF] = 0;
  194.       zap.c_lflag &= ~(ICANON | ECHO);
  195.       ioctl(fileno(stdin), TCSETA, &zap);
  196.       is_fg = TRUE;
  197.       }
  198.    else
  199.       is_fg = FALSE;
  200.    }
  201.  
  202. /* nonblocking_io():
  203.  * try to setup the keyboard to non blocking io
  204.  */
  205. LOCAL void nonblocking_io()
  206.    {
  207.    show = get_pref_scalar(PREF_SHOW);
  208.    /* try to renice our own process to get more cpu time */
  209.    if (!psanity)
  210.       {
  211.       psanity = &sanity;
  212.       ioctl(fileno(stdin), TCGETA, psanity);
  213.       }
  214.    switch_mode();
  215.    at_end(sane_tty);
  216.    }
  217.  
  218.  
  219. /* sane_tty():
  220.  * restores everything to a sane state before returning to shell */
  221. LOCAL void sane_tty()
  222.    {
  223.       ioctl(fileno(stdin), TCSETA, psanity);
  224.    }
  225.  
  226. LOCAL int may_getchar()
  227.    {
  228.    int ch;
  229.  
  230.    if((ch = _read_kbd(0,0,0)) == 0);
  231.        { 
  232.        ch = _read_kbd(0,0,0);
  233.        ch = -1;
  234.        }
  235.    return((ch<0?EOF:ch));
  236.    }
  237.  
  238. LOCAL struct tag result[2];
  239.  
  240. struct tag *get_ui()
  241.    {
  242.    result[0].type = TAG_END;
  243.    result[1].type = TAG_END;
  244.    count_pattern++;
  245.    count_song++;
  246.    switch(may_getchar())
  247.       {
  248.    case 'n':
  249.       result[0].type = UI_NEXT_SONG;
  250.       break;
  251.    case 'p':
  252.       if (count_song > SMALL_DELAY)
  253.          result[0].type = UI_RESTART;
  254.       else
  255.          result[0].type = UI_PREVIOUS_SONG;
  256.       count_song = 0;
  257.       break;
  258.    case 'x':
  259.    case 'e':
  260.    case 'q':
  261.       result[0].type = UI_QUIT;
  262.       break;
  263.    case 's':
  264.       result[0].type = UI_SET_BPM;
  265.       result[0].data.scalar = 50;
  266.       break;
  267.    case 'S':
  268.       result[0].type = UI_SET_BPM;
  269.       result[0].data.scalar = 60;
  270.       break;
  271.    case '>':
  272.       result[0].type = UI_JUMP_TO_PATTERN;
  273.       result[0].data.scalar = current_pattern + 1;
  274.       break;
  275.    case '<':
  276.       result[0].type = UI_JUMP_TO_PATTERN;
  277.       result[0].data.scalar = current_pattern;
  278.       if (count_pattern < SMALL_DELAY)
  279.          result[0].data.scalar--;
  280.       break;
  281.    case '?':
  282.       show = !show;
  283.         set_pref_scalar(PREF_SHOW, show);
  284.         if (show)
  285.             putchar('\n');
  286.       break;
  287.    default:
  288.       break;
  289.       }
  290.    return result;
  291.    }
  292.       
  293.          
  294. #endif
  295.  
  296.  
  297. void notice(s)
  298. char *s;
  299.    {
  300.    fprintf(stderr, "%s\n", s);
  301.    }
  302.  
  303. void status(s)
  304. char *s;
  305.    {
  306.    if (run_in_fg())
  307.       {
  308.         if (s)
  309.          {
  310.          puts(s);
  311.          }
  312.       else
  313.          putchar('\n');
  314.       }
  315.    }
  316.  
  317. LOCAL char title[25];
  318. void song_title(s)
  319. char *s;
  320.    {
  321.     strncpy(title, s, 25);
  322.     if (run_in_fg() && !show)
  323.         puts(title);
  324.    count_song = 0;
  325.    }
  326.  
  327.  
  328. LOCAL char scroll_buffer[80];
  329.  
  330. GENERIC begin_info(title)
  331. char *title;
  332.    {
  333.    if (run_in_fg())
  334.       return scroll_buffer;
  335.    else
  336.       return 0;
  337.    }
  338.  
  339. void infos(handle, s)
  340. GENERIC handle;
  341. char *s;
  342.    {
  343.    if (handle)
  344.       printf(s);
  345.    }
  346.  
  347. void info(handle, line)
  348. GENERIC handle;
  349. char *line;
  350.    {
  351.    if (handle)
  352.       puts(line);
  353.    }
  354.  
  355. void end_info(handle)
  356. GENERIC handle;
  357.    {
  358.    if (handle)
  359.       fflush(stdout);
  360.    }
  361.  
  362. LOCAL char *last_result = 0;
  363.  
  364. char *new_scroll(void)
  365.    {
  366.    if (run_in_fg())
  367.       {
  368.       last_result = scroll_buffer;
  369.       strcpy(scroll_buffer, "             |             |             |             ");
  370.       }
  371.    else
  372.       last_result = 0;
  373.    return last_result;
  374.    }
  375.    
  376. void scroll()
  377.    {
  378.    if (run_in_fg() && last_result)
  379.         {
  380.       puts(scroll_buffer);
  381.         fflush(stdout);
  382.         }
  383.    }
  384.  
  385. void display_pattern(current, total)
  386. int current, total;
  387.    {
  388.    if (run_in_fg())
  389.       {
  390.       if (show)
  391.          printf("\n%3d/%3d %s\n", current, total, title);
  392.       else
  393.          printf("%3d/%3d\b\b\b\b\b\b\b", current, total);
  394.       fflush(stdout); 
  395.       }
  396.    current_pattern = current;
  397.    count_pattern = 0;
  398.    }
  399.  
  400. int checkbrk()
  401.    {
  402.    return FALSE;
  403.    }
  404.